﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using Agro.Library;
using Agro.Services;
using SN.Core;
using System.Data;
using System.Web.Security;
using Agro.Models;
using System.IO;

namespace Agro.Web.Areas.FileManage.Controllers
{
    public class FileController : BaseController
    {
        //
        // GET: /FileManage/File/

        [Inject]
        public IFileService FileService { get; set; }

        public ActionResult FileList()
        {
            return View();
        }
        public ActionResult FileGetList(QueryBase query)
        {
            DataTable dt = FileService.FileGetList(query, null);
            return JqGrid(dt, query);
        }
        public ActionResult AddFile()
        {
            ViewBag.auth = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
            ViewBag.GUID = Guid.NewGuid().ToString().Replace("-", "");
            return View();
        }
        public ActionResult Upload()
        {
            return View();
        }
        public ActionResult SaveFile(string id, FormCollection form)
        {
            T_File file = new T_File();
            file.File_Title = form["File_Title"].ToString();
            file.File_Type = form["File_Type"].ToString();
            file.File_ServerName = form["File_ServerName"].ToString();
            //if (file.File_ID > 0)
            //{
            //    file = FileService.GetEntity<T_File>(T_File._.File_ID == id);
            //}
            //UpdateModel(file, form); //不知道为什么提示，不能更新数据模型(亚磊，你看一下)
            string msg = string.Empty;
            FileService.FileSave(file, out msg);
            return SNJson(file.File_ID, msg);
        }


        public void DownLoadFile(string id)
        {
            string msg = string.Empty;
            string fileName;

            T_File File = new T_File();
            Response.Clear();
            if (string.IsNullOrWhiteSpace(id))
            {
                msg = "请选择要下载的文件！";
                Response.Write("<script>alert('提示:" + msg + "');</script>");
                Response.End();
                return;
            }
            File = FileService.GetEntity<T_File>(T_File._.File_ID == id);

            //处理下载
            Response.Clear();
            string filePath = Server.MapPath("~/Files/" + File.File_ServerName);
            if (!System.IO.File.Exists(filePath))
            {
                msg = "所要下载的文件不存在！";
                Response.Write("<script>alert('提示:" + msg + "');</script>");
                Response.End();
                return;
            }
            fileName = Path.GetFileName(filePath);
            using (FileStream file = new FileStream(filePath, FileMode.Open))
            {
                Response.ContentType = "application/octet-stream";
                //通知浏览器下载文件而不是打开
                //Content-Disposition参数：
                //attachment --- 作为附件下载    
                //inline --- 在线打开


                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(File.File_Title, System.Text.Encoding.UTF8));
                byte[] bytes = new byte[(int)file.Length];
                file.Read(bytes, 0, bytes.Length);
                file.Close();
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }

        }

        [HttpPost]
        public ActionResult DeleteFile(string ids)
        {
            string msg = string.Empty;
            FileService.DeleteFile(ids, out msg);
            return SNJson(msg);
        }

    }
}
